<?php
/**
 * Created by PhpStorm.
 * User: mww
 * Date: 2018/12/17
 * Time: 16:08
 */
namespace app\api\controller;
use think\Db;
class Discuss extends Base
{
    /**
     * 异地登录验证
     * @return string|void
     */
    public function _initialize()
    {
        extract(input());
        if($user_id != 0){
            $auth = $this->checkAll($user_id,$time,$value,$reqKey);
            if($auth == 112){
                echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
                exit;
            }elseif($auth == 110){
                echo json_encode(['status' => 302, 'msg' => $this->frost]);
                exit;
            }elseif($auth == 1103){
                echo json_encode(['status' => 303, 'msg' => $this->del]);
                exit;
            }
        }

    }


    /**
     * 获取社区数据：
     */
    public function getDiscussData(){
        extract(input());
        $where = [
            't_status' => 1
        ];
        if(isset($fid) && $fid){
            $where['t_fid'] = $fid;
        }
        if(isset($keyword) && $keyword){
            $where['t_content'] = ['like',"%{$keyword}%"];
        }
        $field = 'user_id,user_username,user_image';
        $field .= ',t_id,t_type,t_content,t_image,t_comment,t_zan,t_time';
        $data = Db::table('up_talks t')
            ->join('up_u_user u', 't.t_user_id = u.user_id', 'left')
            ->where($where)
            ->order('t_update desc')
            ->page($page,10)
            ->field($field)
            ->select();
        if($data){
            $data = $this->foreachStrArr($data,'t_image','t_type',1,1,$user_id);
        }
        return json(['status'=>200,'msg'=>$data]);
    }
    /**
     * 社区帖子详情：discussInfo
     * 评论回复点赞在资讯那里
     * t_id
     */
    public function discussInfo(){
        extract(input());
        $data = $this->getTableData('up_talks',['t_id' => $t_id]);
        $zan = $this->getTableData('up_zan',['zan_type' => 3,'zan_new_com_id' => $t_id,'zan_user_id' => $user_id]);
        $data['zan'] = 1; //我没有赞
        if($zan){
            $data['zan'] = 2;
        }
        return json(['status'=>200,'msg'=>$data]);
    }
    /**
     * 发布社区帖子
     */
    public function userNewDiscuss(){
        extract(input());
        $today = date('Y-m-d H:i:s');
        $data = [
            't_fid' => $t_fid,
            't_user_id' => $user_id,
            't_type' => $t_type,
            't_content' => $t_content,
            't_image' => trim($t_image,','),
            't_time' => $today,
            't_update' => $today
        ];
        $res = Db::table('up_talks')->insertGetId($data);
        if($res){
            return json(['status' => 200, 'msg' => '发布成功']);
        }else{
            return json(['status' => 0, 'msg' => '发布失败']);
        }
    }
    //----------------------------------专家号----------------------------------
    /**
     * 专家号轮播图
     * 轮播图详情在资讯那里
     */
    public function discussLunbo(){
        extract(input());
        if(!isset($fid) || !$fid){
            $fid = 1;
        }
        //1、轮播
        $data = Db::table('up_banner')
            ->where('banner_type',2)
            ->where('banner_fid',$fid)
            ->where('banner_status',1)
            ->order('banner_sort desc')
            ->field('banner_id,banner_img,banner_title')
            ->select();
        //1、下面的轮播（应该是只有一张）
        $dataDown = Db::table('up_banner')
            ->where('banner_type',3)
            ->where('banner_fid',$fid)
            ->where('banner_status',1)
            ->order('banner_sort desc')
            ->field('banner_id,banner_img,banner_title')
            ->find();
        //2、推荐的专家
        $field = 'user_id,user_username,user_image,user_title,user_tags,user_repay,user_hot';
        $users = Db::table('up_u_user')
            ->where('user_auth',$fid)
            ->where('user_status',1)
            ->page(1,3)
            ->order('user_live desc,user_updatetime desc')
            ->field($field)
            ->select();
        if($users){
            $users = $this->dataLikes($users,$user_id);
        }
        $user_repay = Db::table('up_u_user')
            ->where('user_auth',$fid)
            ->where('user_status',1)
            ->page(1,3)
            ->order('user_repay desc,user_updatetime desc')
            ->field($field)
            ->select();
        $user_hot = Db::table('up_u_user')
            ->where('user_auth',$fid)
            ->where('user_status',1)
            ->page(1,3)
            ->order('user_hot desc,user_updatetime desc')
            ->field($field)
            ->select();
        return json(['status'=>200,'msg'=>$data,'down'=>$dataDown,'users'=>$users,'repays'=>$user_repay,'hots'=>$user_hot]);
    }
    /**
     * 专家推荐和榜单》更多
     * type：1推荐  2盈利榜  3人气榜
     */
    public function userRecommend(){
        extract(input());
        if(!isset($fid) || !$fid){
            $fid = 1;
        }
        if($type == 1) $order = 'user_live desc,user_updatetime desc';
        if($type == 2) $order = 'user_repay desc,user_updatetime desc';
        if($type == 3) $order = 'user_hot desc,user_updatetime desc';
        $field = 'user_id,user_username,user_image,user_title,user_tags,user_repay,user_hot';
        $data = Db::table('up_u_user')
            ->where('user_auth',$fid)
            ->where('user_status',1)
            ->page($page,15)
            ->order($order)
            ->field($field)
            ->select();
        if($data){
            $data = $this->dataLikes($data,$user_id);
        }
        return json(['status'=>200,'msg'=>$data]);
    }
    /**
     * 专家详情
     */
    public function userInfo()
    {
        extract(input());
        $field = 'user_id,user_username,user_image,user_title,user_tags';
        $user = $this->getUserInfo($uid,$field,2);
        if($user['user_tags']){
            $user['user_tags'] = explode(',',$user['user_tags']);
        }else{
            $user['user_tags'] = [];
        }
        //有没有关注
        $user['like'] = 1;
        $have = $this->getTableData('up_like',['like_user_id' => $user_id,'like_other_user' => $uid]);
        if($have){
            $user['like'] = 2;
        }
        //1、数据
        $data = Db::table('up_user_date')
            ->where('ud_user_id',$user_id)
            ->order('ud_order desc')
            ->find();
        $dates = [];
        if($data){
            $dates = json_decode($data['ud_json'],true);
        }
        //2、方案
        $plan = Db::table('up_user_plan')
            ->where('up_user_id',$user_id)
            ->order('up_time desc')
            ->select();
        return json(['status'=>200,'msg'=>$user,'dates'=>$dates,'plans'=>$plan]);
    }
    /**
     * 关注和取消关注
     *
     */
    public function dianzan(){
        extract(input());
        $today = date('Y-m-d H:i:s');
        $have = $this->getTableData('up_like',['like_user_id' => $user_id,'like_other_user' => $other_id]);
        if($have){
            $res = Db::table('up_like')->where('like_id',$have['like_id'])->delete();
        }else{
            $res = Db::table('up_like')->insert([
                'like_user_id' => $user_id,
                'like_other_user' => $other_id,
                'like_time' => $today
            ]);
        }
        if($res){
            return json(['status' => 200, 'msg' => '操作成功']);
        }else{
            return json(['status' => 0, 'msg' => '操作失败']);
        }
    }
}